<?php
/**
 * Created by Engineer CuiLiwu.
 * Project: deal.
 * Date: 2018/7/4-15:37
 * License Hangzhou orce Technology Co., Ltd. Copyright © 2018
 */


namespace App\Http\Controllers\Backend\Project;
use App\ConstDir\ErrorConst;
use App\Http\Controllers\Backend\BaseController;
use App\Repository\Models\Project;
use App\Repository\Repositories\Interfaces\ProjectRepository;
use Illuminate\Http\Request;


class ProjectController  extends BaseController
{
    protected $project_repo;
    protected $articleCate;
    protected $request;
    public function __construct(ProjectRepository $project_repo,Request $request) {
        parent::__construct();
        $this->project_repo=$project_repo;
        $this->request=$request;
    }

    /**
     * 项目信息列表
     * 分页
     * */
    public function index(){

        $data=$this->project_repo->paginate($this->perPage);
        return $this->success(ErrorConst::SUCCESS_CODE, $data, true);
    }

    /**
     * 项目信息详情
     * @param $id
     * @return \Illuminate\Support\Collection
     */
    public function show($id){
        $data=$this->project_repo->with(['region'])->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE, $data);

    }
    /**
     * 项目信息添加
     *
     * */
    public function store(){
        $data=$this->request->all();
        if (isset($data['project_type'])) {
            $data['project_type'] = json_encode($data['project_type']);
        }else{
            $data['project_type'] = json_encode(array());
        }
//        dd($data);
        $ret=$this->project_repo->create($data);
//        dd($ret);
        return $this->success(ErrorConst::SUCCESS_CODE, $data);

    }
    /**
     * 项目信息修改
     *
     * */
    public function update($id){
        $data=$this->request->all();
        if (isset($data['project_type'])) {
            $data['project_type'] = json_encode($data['project_type']);
        }else{
            $data['project_type'] = json_encode(array());
        }
        $ret=$this->project_repo->update($data,$id);
        return $this->success(ErrorConst::SUCCESS_CODE, $ret);
    }

    /**
     * 删除项目信息 无软删除
     *
     * */
    public function delete($id){
        $ret=$this->project_repo->delete($id);
        return $this->success(ErrorConst::SUCCESS_CODE, $ret);
    }
    /**
     * 删除项目信息 无软删除
     *
     * */
    public function setCheckStatus ($id){
        $data = $this->request->get('check_status');
        $ret=$this->project_repo->update(['check_status'=>$data],$id);
        return $this->success(ErrorConst::SUCCESS_CODE, $ret);
    }
    /**
     * 后台-验证分类保存
     * */
    public function form(){
        $this->validate($this->request,[
            'type'  => 'required', // 项目类型必选
//            'announcement_type'  => 'required', // 公告类型必选
            'purchase_mode'  => 'required', // 采购方式必选
            'purchase_type'  => 'required', // 采购类型必选
            'city_id'  => 'required', // 城市必选
            'project_owner'  => 'required', // 业主单位必填
            'contacts'  => 'required', // 联系人必填
            'phone'  => 'required', // 联系电话必填
            'content'  => 'required', //项目信息内容必填
//            'project_type'  => 'required', //项目信息内容必填
            'check_status'  => 'required', //项目信息内容必填

            'title' => 'required|unique:projects,title', // 标题
        ],[
            'type'  => '项目类型必选',
//            'announcement_type'  => '公告类型必选',
            'purchase_mode'  => '采购方式必选', // 采购方式必选
            'purchase_type'  => '采购类型必选', // 采购类型必选
            'city_id'  => '城市必选', // 城市必选
            'project_owner'  => '业主单位必填', // 业主单位必填
            'contacts'  => '联系人必填', // 联系人必填
            'phone'  => '联系电话必填', // 联系电话必填
            'content'  => '项目信息内容必填', //项目信息内容必填
//            'project_type'  => '审核状态必填', //项目信息内容必填
            'check_status'  => '审核状态必填', //项目信息内容必填
            'title' => '标题名称必填',
        ]);

        return $this->request->all(['type','announcement_type','purchase_mode','purchase_type','city','project_owner','contacts','phone','content','check_status','title','project_type']);
    }

}